package com.apusic.controller;

import com.apusic.common.PageInfo;
import com.apusic.entity.*;
import com.apusic.service.*;
import com.apusic.util.Base64Util;
import com.apusic.util.CommonUtils;
import com.apusic.util.JsonUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private UserService userService;
	
	@Autowired
	private BaseVersionService versionService;
	
	@Autowired
	private DistrictService districtService;
	
	@Autowired
	private UserGroupService userGroupService;
	
	@Autowired
	private BaseUserDistrictService baseUserDistrictService;
	
	@Autowired
	private TBaseOrganizeService tbaseOrganizeService;
	
	@Autowired
	private UserGroupRService userGroupRService;
	
	@Autowired
	private VersionGroupRService versionGroupRService;
	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/user_apply")
	public String userList(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/user_apply";
	}
	
	@RequestMapping(value = "/userListData")
	@ResponseBody
	public String userListData(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<TUser> userList = userService.getUser();
		PageInfo<TUser> pageInfo = new PageInfo(userList); 
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	
	@RequestMapping(value = "/stopUserStatus")
	@ResponseBody
	public void stopUserStatus(HttpServletRequest request,
			HttpServletResponse response){
		String userIds = request.getParameter("userIds");
		String[] userIdArray = userIds.split(",");
		for(int i=0;i<userIdArray.length;i++){
			Integer userId = Integer.valueOf(userIdArray[i]);
			userService.updateUserStatus(userId, "0");
		}
	}
	@RequestMapping(value = "/activeUserStatus")
	@ResponseBody
	public void activeUserStatus(HttpServletRequest request,
			HttpServletResponse response){
		String userIds = request.getParameter("userIds");
		String[] userIdArray = userIds.split(",");
		for(int i=0;i<userIdArray.length;i++){
			Integer userId = Integer.valueOf(userIdArray[i]);
			userService.updateUserStatus(userId, "1");
		}
	}
	
	@RequestMapping(value = "/addUserForm")
	public String addUserForm(HttpServletRequest request,
			HttpServletResponse response){
		TBaseDistrict district=new TBaseDistrict();
		district.setParentCode("100000");
		district.setLevel("1");
		List<TBaseDistrict> list = districtService.getDistrictList(district);
		request.setAttribute("provinceList", list);
		return "user/user_add";
	}
	
	@RequestMapping(value = "/saveUser")
	public String saveUser(HttpServletRequest request,
			HttpServletResponse response,TUser user){
		user.setRegisterTime(new Date());
		user.setUserId(CommonUtils.getUUID());
		user.setUserStatus("0");
		user.setUserPassword(Base64Util.encode(user.getUserPassword()));
		String type=user.getType();
		String province=user.getProvince();
		String city=user.getCity();
		String county=user.getCounty();
		if(type!=null&&"3".equals(type)){
			user.setDistrictCode(user.getMemberId());
			user.setMemberId("");
		}else{
			if(county!=null&&!county.equals("请选择")){
				user.setDistrictCode(county);
			}else if(city!=null&&!city.equals("请选择")){
				user.setDistrictCode(city);
			}else if(province!=null&&!province.equals("请选择")){
				user.setDistrictCode(province);
			}
		}
		
		userService.saveUser(user);
		return "redirect:/user/user_apply";
	}
	@RequestMapping(value = "/saveUserDistrict")
	public String saveUserDistrict(HttpServletRequest request,
			HttpServletResponse response,String userId,String userName,String province,String provinceName,String city,String cityName,String county,String countyName){
		TBaseUserDistrict baseUserDistrict=new TBaseUserDistrict();
		if(county!=null&&!county.equals("请选择")){
			baseUserDistrict.setDistrictId(county);
			baseUserDistrict.setDistrictName(countyName);
		}else if(city!=null&&!city.equals("请选择")){
			baseUserDistrict.setDistrictId(city);
			baseUserDistrict.setDistrictName(cityName);
		}else if(province!=null&&!province.equals("请选择")){
			baseUserDistrict.setDistrictId(province);
			baseUserDistrict.setDistrictName(provinceName);
		}
		baseUserDistrict.setUserId(userId);
		baseUserDistrict.setUserName(userName);
		baseUserDistrict.setId(10);
		baseUserDistrictService.saveUserDistrict(baseUserDistrict);
		return "redirect:/user/user_apply";
	}
	
	/**
	 * 验证是否用户名重复
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/sameUserAccountValidate")
	@ResponseBody
	public String sameUserAccountValidate(HttpServletRequest request,
			HttpServletResponse response,TUser user) {
		List<TUser> userList = userService.selectByObj(user);
		String str = JsonUtils.objectToJson(userList);
		return str;
	}
	
	/**
	 * 验证是否用户组名称重复
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/sameGroupAccountValidate")
	@ResponseBody
	public String sameGroupAccountValidate(HttpServletRequest request,
			HttpServletResponse response,UserGroup group) {
		List<UserGroup> userGroupList = userGroupService.selectByObj(group);
		String str = JsonUtils.objectToJson(userGroupList);
		return str;
	}
	
	/**
	 * 用户权限列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userDistrict")
	public String userDistrict(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/userDistrict";
	}
	
	/**
	 * 用户组列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userGroup")
	public String userGroup(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/userGroup_apply";
	}
	
	/**
	 * 组织列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userOrganize")
	public String userOrganize(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/organize_apply";
	}
	
	/**
	 * 用户组新增页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addUserGroup")
	public String addUserGroup(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/userGroup_add";
	}
	
	/**
	 * 查询用户权限列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userDistrictData")
	@ResponseBody
	public String userDistrictData(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TBaseUserDistrict userDistrict=new TBaseUserDistrict();
		List<TBaseUserDistrict> userDistrictList = baseUserDistrictService.selectByObj(userDistrict);
		PageInfo<TUser> pageInfo = new PageInfo(userDistrictList); 
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	/**
	 * 查询用户组列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userGroupData")
	@ResponseBody
	public String userGroupData(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		UserGroup group=new UserGroup();
		List<UserGroup> userGroupList = userGroupService.selectByObj(group);
		PageInfo<TUser> pageInfo = new PageInfo(userGroupList);
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	@RequestMapping(value = "/saveUserGroup")
	public String saveUserGroup(HttpServletRequest request,
			HttpServletResponse response,UserGroup group){
		userGroupService.saveUserGroup(group);
		return "redirect:/user/userGroup";
	}
	/*
	 * 新增用户权限页面
	 */
	@RequestMapping(value = "/addUserDistrictForm")
	public String addUserDistrictForm(HttpServletRequest request,
			HttpServletResponse response){
		//区域下拉框
		TBaseDistrict district=new TBaseDistrict();
		district.setParentCode("100000");
		district.setLevel("1");
		List<TBaseDistrict> list = districtService.getDistrictList(district);
		request.setAttribute("provinceList", list);
		
		//用户下拉框
		List userList=userService.selectByObj(new TUser());
		request.setAttribute("userList", userList);
		return "user/addUserDistrict";
	}
	
	/**
	 * 用户组查询方法
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/userOrgList")
	public String userOrgList(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="10") int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<TBaseOrganize> organizeList = tbaseOrganizeService.getOrganizeList();
		PageInfo<TUser> pageInfo = new PageInfo(organizeList); 
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	/**
	 * 组织新增页面转跳方法
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addOrganize")
	public String AddOrganize(HttpServletRequest request,
			HttpServletResponse response){
		
	     //区域下拉框
		TBaseDistrict district=new TBaseDistrict();
		district.setParentCode("100000");
		district.setLevel("1");
		List<TBaseDistrict> list = districtService.getDistrictList(district);
		request.setAttribute("provinceList", list);
		return "user/organize_add";
	}
	
	
	@RequestMapping(value = "/saveOrganize")
	public void saveOrganize(HttpServletRequest request,
			HttpServletResponse response,String province,String city,String county,String organizeName){
		TBaseOrganize tbaseOrganize = new TBaseOrganize();
		if(county!=null&&!county.equals("请选择")){
			tbaseOrganize.setDistrictCode(county);
		}else if(city!=null&&!city.equals("请选择")){
			tbaseOrganize.setDistrictCode(city);
		}else if(province!=null&&!province.equals("请选择")){
			tbaseOrganize.setDistrictCode(province);
		}
		tbaseOrganize.setOrganizeName(organizeName);
		tbaseOrganizeService.saveOrganize(tbaseOrganize);
	}
	
	/**
	 * 查询组织方法
	 * @param districtCode
	 * @return
	 */
	@RequestMapping(value = "/getOrg")
	@ResponseBody
	public String getOrg(String districtCode){
		if(districtCode!=null&&districtCode!=""){
			TBaseOrganize  tbaseOrganize= new TBaseOrganize();
			tbaseOrganize.setDistrictCode(districtCode);
			List<TBaseOrganize> orgJson = tbaseOrganizeService.getOrgJson(tbaseOrganize);
			String objectToJson = JsonUtils.objectToJson(orgJson);
			return objectToJson;
		}
		return "";
	}
	
	/**
	 * 用户-组配置列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userGroupR_apply")
	public String userGroupR_apply(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/userGroupR_apply";
	}
	
	/**
	 * 查询用户-组配置信息
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/queryUserGroupR")
	@ResponseBody
	public String queryUserGroupR(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize,UserGroupR group) {
		PageHelper.startPage(pageNum, pageSize);
		List<UserGroupR> userGroupRList = userGroupRService.selectByObj(group);
		PageInfo<TUser> pageInfo = new PageInfo(userGroupRList); 
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	/**
	 * 用户-组配置新增页面转跳方法
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addUserGroupR")
	public String addUserGroupR(HttpServletRequest request,
			HttpServletResponse response){
	     TUser user=new TUser();
	     UserGroup group =new UserGroup();
	     List userList=userService.selectByObj(user);
	     List groupList=userGroupService.selectByObj(group);
	     request.setAttribute("userList", userList);
	     request.setAttribute("groupList", groupList);
		return "user/userGroupR_add";
	}
	
	/**
	 * 查询用户-组配置
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/queryUserGroupRList")
	@ResponseBody
	public String queryUserGroupRList(HttpServletRequest request,
			HttpServletResponse response,UserGroupR group) {
		List<UserGroupR> UserGroupRList = userGroupRService.selectByObj(group);
		String str = JsonUtils.objectToJson(UserGroupRList);
		return str;
	}
	/**
	 * 保存用户-组配置
	 * @param request
	 * @param response
	 * @param group
	 * @return
	 */
	@RequestMapping(value = "/saveUserGroupR")
	public String saveUserGroupR(HttpServletRequest request,
			HttpServletResponse response,UserGroupR group){
		userGroupRService.saveUserGroupR(group);
		return "redirect:/user/userGroupR_apply";
	}
	
	/**
	 * 修改用户-组配置信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateUserGroupR")
	public String updateUserGroupR(HttpServletRequest request,
			HttpServletResponse response,UserGroupR group){
		userGroupRService.updateUserGroupR(group);
		return "redirect:/user/userGroupR_apply";
	}
	
	/**
	 * 修改用户-组配置页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userGroupREdit")
	public String userGroupREdit(HttpServletRequest request,
			HttpServletResponse response,String userGroupRId){
		
	     UserGroup group =new UserGroup();
	     List groupList=userGroupService.selectByObj(group);
	     request.setAttribute("groupList", groupList);
	     request.setAttribute("userGroupRId",userGroupRId);
		return "/user/userGroupR_edit";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 版本-组配置列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/versionGroupR_apply")
	public String versionGroupR_apply(HttpServletRequest request,
			HttpServletResponse response) {
		return "user/versionGroupR_apply";
	}
	
	/**
	 * 查询版本-组配置信息
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/queryVersionGroupR")
	@ResponseBody
	public String queryVersionGroupR(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize,VersionGroupR group) {
		PageHelper.startPage(pageNum, pageSize);
		List<VersionGroupR> versionGroupRList = versionGroupRService.selectByObj(group);
		PageInfo<TUser> pageInfo = new PageInfo(versionGroupRList); 
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
	}
	
	/**
	 * 版本-组配置新增页面转跳方法
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addVersionGroupR")
	public String addVersionGroupR(HttpServletRequest request,
			HttpServletResponse response){
	     BaseVersion version=new BaseVersion();
	     UserGroup group =new UserGroup();
	     List versionList=versionService.selectByObj(version);
	     List groupList=userGroupService.selectByObj(group);
	     request.setAttribute("versionList", versionList);
	     request.setAttribute("groupList", groupList);
		return "user/versionGroupR_add";
	}
	
	/**
	 * 查询版本-组配置
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/queryVersionGroupRList")
	@ResponseBody
	public String queryVersionGroupRList(HttpServletRequest request,
			HttpServletResponse response,VersionGroupR group) {
		List<VersionGroupR> VersionGroupRList = versionGroupRService.selectByObj(group);
		String str = JsonUtils.objectToJson(VersionGroupRList);
		return str;
	}
	/**
	 * 保存用户-组配置
	 * @param request
	 * @param response
	 * @param group
	 * @return
	 */
	@RequestMapping(value = "/saveVersionGroupR")
	public String saveVersionGroupR(HttpServletRequest request,
			HttpServletResponse response,VersionGroupR group){
		versionGroupRService.saveVersionGroupR(group);
		return "redirect:/user/versionGroupR_apply";
	}
	
	/**
	 * 修改版本-组配置信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateVersionGroupR")
	public String updateVersionGroupR(HttpServletRequest request,
			HttpServletResponse response,VersionGroupR group){
		versionGroupRService.updateVersionGroupR(group);
		return "redirect:/user/versionGroupR_apply";
	}
	
	/**
	 * 修改用户-组配置页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/versionGroupREdit")
	public String versionGroupREdit(HttpServletRequest request,
			HttpServletResponse response,String versionGroupRId){
		BaseVersion version=new BaseVersion();
		List versionList=versionService.selectByObj(version);
	     request.setAttribute("versionList", versionList);
	     request.setAttribute("versionGroupRId",versionGroupRId);
		return "/user/versionGroupR_edit";
	}
}
